Crate subxt_codegen

source ·
Expand description

Library to generate an API for a Substrate runtime from its metadata.

Generated Structure

The API generator logic:

  • At the root there is the item_mod provided (ie pub mod api {})
  • Pallets are represented by a child module (ie pub mod PalletName {}) of the root
  • Each pallet exposes as child modules (if applicable):
    • Calls (pub mod calls {})
    • Events (pub mod events {})
    • Storage (pub mod storage {})
    • Constants (pub mod constants {})

Example

use std::fs;
use codec::Decode;
use frame_metadata::RuntimeMetadataPrefixed;
use subxt_codegen::{CratePath, DerivesRegistry, TypeSubstitutes};

let encoded = fs::read("../artifacts/polkadot_metadata.scale").unwrap();

// Runtime metadata obtained from a node.
let metadata = <RuntimeMetadataPrefixed as Decode>::decode(&mut &*encoded).unwrap();
// Module under which the API is generated.
let item_mod = syn::parse_quote!(
    pub mod api {}
);
// Default module derivatives.
let mut derives = DerivesRegistry::new(&CratePath::default());
// Default type substitutes.
let substs = TypeSubstitutes::new(&CratePath::default());
// Generate the Runtime API.
let generator = subxt_codegen::RuntimeGenerator::new(metadata);
let runtime_api = generator.generate_runtime(item_mod, derives, substs, CratePath::default());
println!("{}", runtime_api);

Modules

Macros

Structs

Represents a Rust mod, containing generated types and child mods.
Create the API for interacting with a Substrate runtime.
Generate a Rust module containing all types defined in the supplied PortableRegistry.

Functions

Generates the API for interacting with a substrate runtime, using metadata bytes.
Generates the API for interacting with a Substrate runtime.
Generates the API for interacting with a substrate runtime, using metadata that can be downloaded from a node at the provided URL. This function blocks while retrieving the metadata.